package com.ztrue.framework.core.domain.impl;

import com.ztrue.framework.cache.CacheProvider;
import com.ztrue.framework.core.domain.IAggregateRoot;
import com.ztrue.framework.core.domain.IAggregateRootCache;
import com.ztrue.common.properties.Proper;

/**
 * 聚合根缓存
 *
 * @author zhangw
 * @date 2016/11/2 15:22
 * @version: 1.0
 */
public class AggregateRootCache implements IAggregateRootCache {

    private final int cacheExpired;
    private final String GROUP = "AGGREGATEROOT";

    public AggregateRootCache() {
        cacheExpired = Proper.getInt("ztrue.aggregate.cache.expired", 300);
    }

    /**
     * 缓存一个聚合根对象
     *
     * @param aggregateRoot
     */
    @Override
    public void set(IAggregateRoot aggregateRoot) {
        CacheProvider.setCache(GROUP, aggregateRoot.getUniqueId(), aggregateRoot, cacheExpired);
    }

    /**
     * 根据聚合根ID从缓存中获取一个聚合根对象
     *
     * @param id
     * @return
     */
    @Override
    public <T extends IAggregateRoot> T get(String id) {
        return CacheProvider.<T>getCache(GROUP, id);
    }
}
